package homework03;

import java.io.*;

public class DataDemo {
    public static void copy(String srcPath,String destPath) {
        if(srcPath == null ||"".equals(srcPath)) {
            throw new NullPointerException();
        }
        if(destPath == null ||"".equals(destPath)) {
            throw new NullPointerException();
        }
        DataInputStream dis = null;
        DataOutputStream dos = null;
        try {
            dis = new DataInputStream(new FileInputStream(srcPath));
            File destFile = new File(destPath);
            if(!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }
            dos = new DataOutputStream(new FileOutputStream(destPath));
            int len = -1;
            byte[] buf = new byte[8192];
            while((len = dis.read(buf)) >= 0) {
                dos.write(buf,0,len);
                dos.flush();
            }
            System.out.println("复制成功!");
        } catch (Exception e) {
            e.getStackTrace();
        }finally {
            if(dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        copy("F:\\IJ\\Text02\\HomeWork1016\\doc\\1.txt","F:\\IJ\\Text02\\HomeWork1016\\doc\\2.txt");
    }
}
